package com.risetechnology.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;

import com.risetechnology.customviews.CustomFontTextView;
import com.risetechnology.data.Method;
import com.risetechnology.krystalkitchen1.R;

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
	
	private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<Method>> _listDataChild;
 
    public CustomExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<Method>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
 
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
 
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
 
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
 
        final String childText = (childPosition+1) + ": " + ((Method) getChild(groupPosition, childPosition)).getContent();
 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item_method, null);
        }
 
        CustomFontTextView txtListChild = (CustomFontTextView) convertView
                .findViewById(R.id.tv_method);
 
        txtListChild.setText(childText);
        
        final View tempView = convertView;
        convertView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (((Method) getChild(groupPosition, childPosition)).isSelected()) {
					((Method) getChild(groupPosition, childPosition)).setSelected(false);
					tempView.setBackgroundColor(Color.TRANSPARENT);
				} else {
					((Method) getChild(groupPosition, childPosition)).setSelected(true);
					tempView.setBackgroundColor(_context.getResources().getColor(R.color.bg_calendar_item_inactive));
				}
			}
		});
        
        return convertView;
    }
 
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }
 
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
 
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
 
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
 
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.layout_group_name, null);
        }
 
        CustomFontTextView lblListHeader = (CustomFontTextView) convertView
                .findViewById(R.id.tv_group_name);
 
        if(headerTitle==null||headerTitle.length()==0 || headerTitle.equals("Part 1 Name...")
        		|| headerTitle.equals("Part 2 Name...") || headerTitle.equals("Part 3 Name...")){
        	lblListHeader.setText("");
        	lblListHeader.setVisibility(View.GONE);
        	convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        }else{
            lblListHeader.setText(headerTitle);
        	lblListHeader.setVisibility(View.VISIBLE);
        }
        
        return convertView;
    }
 
    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    
}
